classdef ParseRawSBEData < ParseData
  % Intermediate class between ParseData and instrument-specific SBE raw data
  %     parsing classes
  % Derived from ParseData
  % C-tor sig: parser = ParseRawSBEData(dataFilename, xmlconFilename),
  %                     args required, but both may be empty
  %    dataFilename: path of main, i.e., hex data file
  %    xmlconFilename: path of xmlcon, i.e., SBE-type configuration file
  %
  % Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
  % Release date: 9/15/2011
    
    properties
      % Inherited from ParseData
      % dataFilename
        xmlconFilename  = '';       % full (perhaps relative) path of CTD xmlcon file
        instHeaderLines = '';       % (will be a) cell array of strings, from the header, containing instrument data
                                    % in the file these lines are preceded by a '*'
        procHeaderLines = '';       % (will be a) cell array of strings, from the header, containing processed data
                                    % in the file these lines are preceded by a '#'
        dataLines       = {};       % cell array of strings, from the "body" of the file, 
                                    % containing the raw data; these lines are
                                    % preceded by neither of the above
    end
    
    methods
        function parser = ParseRawSBEData(dataFilename, xmlconFilename) % Constructor
            parser = parser@ParseData(dataFilename);
            parser.xmlconFilename = xmlconFilename;
        end
        
        function readDataFile(obj)
            fid = fopen(obj.dataFilename, 'rt');
            line = fgetl(fid);
            while ischar(line)
                line = strtrim(line);
                if isempty(line) || ...
                   isempty(strrep(line, '*', ''))    || ...
                   isempty(strrep(line, '#', ''))    || ...
                   isempty(strrep(line, 'Y', ''))    || ...
                   isempty(strrep(line, '* S>', '')) || ...
                   isempty(strrep(line, '*END*', ''))
                    % do nothing
                elseif line(1) == '*'
                    obj.instHeaderLines ... 
                        = [obj.instHeaderLines strtrim(strrep(line,'*',''))];
                elseif line(1) == '#'
                    obj.procHeaderLines ... 
                        = [obj.procHeaderLines strtrim(strrep(line,'#',''))];
                else
                    obj.dataLines{end+1} = line;
                end
                line  = fgetl(fid);
            end
            fclose(fid);
        end
        
        function readConfigFiles(obj)
            global xmlconData
            xmlconData = myReadXMLFile(obj.xmlconFilename);
        end
        
    end
    
    methods(Abstract)
        parseInstHeader(obj);
        parseProcHeader(obj);
        convertHexData(obj);
        unpackConfigDat(obj);
    end
    
end